//index.js
//获取应用实例
const app = getApp()
import api from '../../utils/api.js';
const {request} =require('../../utils/wx-api.js');
console.log()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    banner: [],
    channel:[],
    hotGood:[],
    newGoodsList:[],
    categoryList:[],
    channel_icon:[
      "http://fanfanda.gitee.io/wechatshop/assets/image/home.png",
      "http://fanfanda.gitee.io/wechatshop/assets/image/cook.png",
      "http://fanfanda.gitee.io/wechatshop/assets/image/parts.png",
      "http://fanfanda.gitee.io/wechatshop/assets/image/clothes.png",
      "http://fanfanda.gitee.io/wechatshop/assets/image/Interest.png"
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular:true
  },
  clickDetails(e){
    console.log(e)
    wx.navigateTo({
      url: '../goodsDetails/details?id=' + e.currentTarget.dataset.id
    })
  },

  // ajax请求
  getData() {
    //读取缓存数据
    let indexData = wx.getStorageSync("indexData");
    // 存在本地缓存
    if(indexData){
      // 使用本地缓存
      console.log(indexData)
      this.setData({
        banner: indexData.banner,
        channel: indexData.channel,
        hotGood: indexData.hotGoodsList,
        newGoodsList: indexData.newGoodsList,
        categoryList: indexData.categoryList
      })
      
    }  //不存在本地缓存
    else{
      // wx.request({
      //   url: api.index,
      //   success: (res) => {
      //     console.log(res)
      //     this.setData({
      //       banner: res.data.data.banner,
      //       channel: res.data.data.channel
      //     });
      //     // 设置缓存数据(同步接口)
      //     wx.setStorageSync("indexData", res.data.data)
          
      //   }
      // })
      request({url:api.index})
      .then(res=>{
        console.log(res);
        wx.setStorageSync("indexData", res.data.data)
        this.setData({
          banner: res.data.data.banner,
          channel: res.data.data.channel,
          hotGood: res.data.data.hotGoodsList,
          newGoodsList: res.data.data.newGoodsList,
          categoryList: res.data.data.categoryList
          });
      })
    }
   
    
  },
  onLoad: function () {
    this.getData()
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  // 轮播图处理函数
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
